//
//  CZQMyDescripView.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/15.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQMyDescripView.h"

@implementation CZQMyDescripView
- (instancetype)initWithFrame:(CGRect)frame
{
    
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.4];
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSWhiteColor;
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"自我描述";
        titleLabel.font = BoldBigFont;
        [whiteView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UIButton *closeBtn = [[UIButton alloc] init];
        [closeBtn setImage:@"pop_icon_close"];
        [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
        [whiteView addSubview:closeBtn];
        self.closeBtn = closeBtn;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:lineView];
        self.lineView = lineView;
        
        CZQCustonTextView *textView = [CZQCustonTextView new];
        [whiteView addSubview:textView];
        self.textView = textView;
        
        MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
        [whiteView addSubview:bottomView];
        self.bottomView = bottomView;
        
        __weak __typeof(self) weakSelf = self;
        
        self.bottomView.cancelBlock = ^ {
            
            [weakSelf hiddenWithAnimation];
            
            if (weakSelf.cancelBlock) {
                
                weakSelf.cancelBlock();
            }
            
        };
        
        self.bottomView.commitBlock = ^ {
            
            
            
            if (weakSelf.commitBlock) {
                
                NSLog(@"%@",self.textView.textView.text);
                
                weakSelf.commitBlock(self.textView.textView.text);
                
                [weakSelf hiddenWithAnimation];
                
            }
        };
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints
{
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(20);
        make.centerX.mas_equalTo(self.whiteView);
    }];
    
    [self.closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.whiteView).offset(-10);
        make.top.mas_equalTo(self.whiteView).offset(10);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(30);
        make.left.width.mas_equalTo(self.whiteView);
        make.height.mas_equalTo(1);
    }];
    
    [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(0);
        
        make.left.mas_equalTo(self.whiteView).offset(10);
        
        make.right.mas_equalTo(self.whiteView).offset(-10);
        
        make.height.mas_equalTo(100);
    }];
    
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.textView.mas_bottom);
        
        make.left.width.bottom.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.whiteView);
    }];
    
    
}

- (void)showWithAnimation {
    [self addViews];
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    CGFloat height = self.whiteView.frame.size.height;
    self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
